<template>
  <div class="swiper-container" id="swiper-container">
    <div class="swiper-wrapper">
      <div v-for="(item, i) in state.imgs" :key="i" class="swiper-slide"><img :src="item.pic"></div>
    </div>
    <!-- 如果需要分页器 -->
    <div class="swiper-pagination"></div>
  </div>
</template>

<script>
import Swiper from "swiper";
import 'swiper/css/swiper.css'
import {getBanner} from "@/api/index"
import {reactive,onMounted,onUpdated} from "vue";

export default {
  setup(){
    let state=reactive({
      imgs:[]
    })
    onMounted(async ()=>{
      let res=await getBanner()
      state.imgs=res.data.banners;
    })
    onUpdated(()=>{
      var mySwiper = new Swiper('#swiper-container', {
        loop: true, // 循环模式选项
        autoplay : true, //自动切换
        // 如果需要分页器
        pagination: {
          el: '.swiper-pagination',
        },
        clickable:true
      });
    })
    return {
      state
    }
  }
}

// export default {
//   data(){
//     return {
//       imgs:[]
//     }
//   },
//   async mounted() {
//     let res=await getBanner()
//     this.imgs=res.data.banners;
//   },
//   updated() {
//     var mySwiper = new Swiper('#swiper-container', {
//       loop: true, // 循环模式选项
//       autoplay : true, //自动切换
//       // 如果需要分页器
//       pagination: {
//         el: '.swiper-pagination',
//       },
//       clickable:true
//     });
//   }
// }
</script>

<style lang="less">
#swiper-container {
  width: 7.1rem;
  height: 3rem;
  .swiper-slide{
    img{
      width: 100%;
      height: auto;
      border-radius: .1rem;
    }
  }
}
.swiper-pagination-bullet-active{
  background-color: #b03b3b!important;
}
</style>